package tuan.travel;

import java.util.*;

import org.w3c.dom.Node;

public class TextNodeTableTravel implements TravelCallback {
	List<Node> tdNodeList = new ArrayList<Node>();
	List<Node> textNodeList = new ArrayList<Node>();
	Node currentTdNode = null;

	public void callback(Node root, Node node) {
		Node pNode = getTdNode(node);
		if (pNode != null && !pNode.equals(currentTdNode) && textNodeList.size() > 0) {
			Node lastTdNode = textNodeList.get(0);
			if (lastTdNode.equals(pNode)) {
				return;
			}
			textNodeList.add(lastTdNode);
			textNodeList.remove(0);
		}
		textNodeList.add(node);
	}

	Node getTdNode(Node node) {
		while (node != null && !node.getNodeName().equals("TD")) {
			node = node.getParentNode();
		}
		return node;
	}

}
